/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * editPostScreen.java
 *
 * Created on Apr 7, 2011, 7:24:46 PM
 */

package forumGUI;

import client.TcpClient;
import forum.Post;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import messaage.EditPostMsg;

/**
 *
 * @author Olga
 */
public class editPostScreen extends javax.swing.JFrame {

    private TcpClient client;
    private String username;
    private JFrame previousFrame;
    private Post postToEdit;
    private String forumID;

    /** Creates new form editPostScreen */
    public editPostScreen(TcpClient _client, String _username, JFrame _parentWindow, Post p, int _forumID) {
        initComponents();
        this.setDefaultCloseOperation(editPostScreen.DO_NOTHING_ON_CLOSE);
        this.subjectofPostLabelr.setText(p.getSubject());
        this.postContentTextArea.setText(p.getContent());
        
        this.client = _client;
        this.username = _username;
        this.previousFrame = _parentWindow;
        this.postToEdit = p;
        this.forumID = _forumID+"";
        this.setBackground();


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        editPostLable = new javax.swing.JLabel();
        subjectLabel = new javax.swing.JLabel();
        editTextLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        postContentTextArea = new javax.swing.JTextArea();
        SendButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();
        subjectofPostLabelr = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        editPostLable.setFont(new java.awt.Font("Viner Hand ITC", 1, 24)); // NOI18N
        editPostLable.setText("Edit post");

        subjectLabel.setFont(new java.awt.Font("Viner Hand ITC", 0, 24)); // NOI18N
        subjectLabel.setText("Subject:");

        editTextLabel.setFont(new java.awt.Font("Viner Hand ITC", 0, 24)); // NOI18N
        editTextLabel.setText("Edit your text here:");

        postContentTextArea.setColumns(20);
        postContentTextArea.setRows(5);
        jScrollPane1.setViewportView(postContentTextArea);

        SendButton.setFont(new java.awt.Font("Tempus Sans ITC", 1, 18)); // NOI18N
        SendButton.setText("Send!");
        SendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SendButtonActionPerformed(evt);
            }
        });

        backButton.setFont(new java.awt.Font("Tempus Sans ITC", 1, 18)); // NOI18N
        backButton.setText("Back");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        subjectofPostLabelr.setFont(new java.awt.Font("Tempus Sans ITC", 1, 12)); // NOI18N
        subjectofPostLabelr.setText("set subject here");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(subjectLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 81, Short.MAX_VALUE)
                                .addComponent(subjectofPostLabelr, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(SendButton)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(backButton))
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(108, 108, 108)
                        .addComponent(editPostLable, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(25, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(editTextLabel)
                .addContainerGap(152, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(editPostLable)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(subjectLabel)
                    .addComponent(subjectofPostLabelr, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addComponent(editTextLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SendButton)
                    .addComponent(backButton))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
         this.previousFrame.setEnabled(true);
        this.setVisible(false);
    }//GEN-LAST:event_backButtonActionPerformed

    private void SendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SendButtonActionPerformed
        
        String newContent = this.postContentTextArea.getText();
        if (newContent ==null || newContent.isEmpty()){
             JOptionPane.showMessageDialog(this, "Content cannot be empty");
             return;
        }
        this.client.sendMessage(new EditPostMsg(this.username, this.forumID+"", this.postToEdit.getID()+"", newContent));
        this.previousFrame.setEnabled(true);
        this.setVisible(false);
        System.out.println("EditPostMsg was sent");
    }//GEN-LAST:event_SendButtonActionPerformed

          public void setBackground()
    {
        Container con =this.getContentPane();
        JPanel panelBgImg;
        con.setLayout(null);
        final String photoPath = "src/images/screen.jpg";
        ImageIcon imh = new ImageIcon(photoPath);
//        setSize(imh.getIconWidth(), imh.getIconHeight());

        panelBgImg = new JPanel()
        {
            public void paintComponent(Graphics g)
            {
                Image img = new ImageIcon(photoPath).getImage();

                Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
                setPreferredSize(size);
                setMinimumSize(size);
                setMaximumSize(size);
                setSize(size);
                setLayout(null);
                g.drawImage(img, 0, 0, null);
            }
        };

        con.add(panelBgImg);
        panelBgImg.setBounds(0, 0, imh.getIconWidth(), imh.getIconHeight());

    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
               // new editPostScreen().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton SendButton;
    private javax.swing.JButton backButton;
    private javax.swing.JLabel editPostLable;
    private javax.swing.JLabel editTextLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea postContentTextArea;
    private javax.swing.JLabel subjectLabel;
    private javax.swing.JLabel subjectofPostLabelr;
    // End of variables declaration//GEN-END:variables

}
